{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FaceRank 预训练模型测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers.core import Dense, Dropout, Flatten, Activation\n",
    "from keras.layers.convolutional import Conv2D, MaxPooling2D\n",
    "from keras.preprocessing.image import load_img, img_to_array\n",
    "from keras.utils import np_utils\n",
    "import os\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_dataset(filedir):\n",
    "    \"\"\"\n",
    "    读取数据\n",
    "    :param filedir:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    image_data_list = []\n",
    "    label = []\n",
    "    train_image_list = os.listdir(filedir + '/train')\n",
    "    for img in train_image_list:\n",
    "        url = os.path.join(filedir + '/train/' + img)\n",
    "        image = load_img(url, target_size=(128, 128))\n",
    "        image_data_list.append(img_to_array(image))\n",
    "        label.append(img.split('-')[0])\n",
    "    img_data = np.array(image_data_list)\n",
    "    img_data = img_data.astype('float32')\n",
    "    img_data /= 255\n",
    "    return img_data, label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def make_network():\n",
    "    model = Sequential()\n",
    "    model.add(Conv2D(32, (3, 3), padding='same', input_shape=(128, 128, 3)))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Conv2D(32, (3, 3)))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "    model.add(Dropout(0.5))\n",
    "\n",
    "    model.add(Flatten())\n",
    "    model.add(Dense(128))\n",
    "    model.add(Activation('relu'))\n",
    "    model.add(Dropout(0.5))\n",
    "    model.add(Dense(11))\n",
    "    model.add(Activation('softmax'))\n",
    "\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_x, train_y = load_dataset('data')\n",
    "train_y = np_utils.to_categorical(train_y)\n",
    "model = make_network()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "120/120 [==============================] - 8s - loss: 2.5834 - acc: 0.1000     \n",
      "Epoch 2/100\n",
      "120/120 [==============================] - 8s - loss: 2.4510 - acc: 0.0667     \n",
      "Epoch 3/100\n",
      "120/120 [==============================] - 9s - loss: 2.3724 - acc: 0.1500     \n",
      "Epoch 4/100\n",
      "120/120 [==============================] - 9s - loss: 2.3495 - acc: 0.0917     \n",
      "Epoch 5/100\n",
      "120/120 [==============================] - 9s - loss: 2.3209 - acc: 0.1917     \n",
      "Epoch 6/100\n",
      "120/120 [==============================] - 9s - loss: 2.3506 - acc: 0.1083     \n",
      "Epoch 7/100\n",
      "120/120 [==============================] - 9s - loss: 2.2856 - acc: 0.1167     \n",
      "Epoch 8/100\n",
      "120/120 [==============================] - 9s - loss: 2.3031 - acc: 0.1667     \n",
      "Epoch 9/100\n",
      "120/120 [==============================] - 9s - loss: 2.2044 - acc: 0.2083     \n",
      "Epoch 10/100\n",
      "120/120 [==============================] - 9s - loss: 2.2749 - acc: 0.1250     \n",
      "Epoch 11/100\n",
      "120/120 [==============================] - 9s - loss: 2.2521 - acc: 0.1833     \n",
      "Epoch 12/100\n",
      "120/120 [==============================] - 9s - loss: 2.1797 - acc: 0.2417     \n",
      "Epoch 13/100\n",
      "120/120 [==============================] - 9s - loss: 2.2485 - acc: 0.1667     \n",
      "Epoch 14/100\n",
      "120/120 [==============================] - 9s - loss: 2.1828 - acc: 0.2000     \n",
      "Epoch 15/100\n",
      "120/120 [==============================] - 9s - loss: 2.0940 - acc: 0.2583     \n",
      "Epoch 16/100\n",
      "120/120 [==============================] - 9s - loss: 2.1489 - acc: 0.2333     \n",
      "Epoch 17/100\n",
      "120/120 [==============================] - 9s - loss: 2.1168 - acc: 0.2167     \n",
      "Epoch 18/100\n",
      "120/120 [==============================] - 9s - loss: 2.0267 - acc: 0.2750     \n",
      "Epoch 19/100\n",
      "120/120 [==============================] - 9s - loss: 2.0205 - acc: 0.2917     \n",
      "Epoch 20/100\n",
      "120/120 [==============================] - 9s - loss: 2.0279 - acc: 0.2667     \n",
      "Epoch 21/100\n",
      "120/120 [==============================] - 9s - loss: 1.8000 - acc: 0.3583     \n",
      "Epoch 22/100\n",
      "120/120 [==============================] - 9s - loss: 1.9757 - acc: 0.2583     \n",
      "Epoch 23/100\n",
      "120/120 [==============================] - 9s - loss: 1.7208 - acc: 0.4417     \n",
      "Epoch 24/100\n",
      "120/120 [==============================] - 9s - loss: 1.7918 - acc: 0.3750     \n",
      "Epoch 25/100\n",
      "120/120 [==============================] - 9s - loss: 1.7776 - acc: 0.3667     \n",
      "Epoch 26/100\n",
      "120/120 [==============================] - 9s - loss: 1.6400 - acc: 0.4250     \n",
      "Epoch 27/100\n",
      "120/120 [==============================] - 9s - loss: 1.6489 - acc: 0.4333     \n",
      "Epoch 28/100\n",
      "120/120 [==============================] - 9s - loss: 1.4964 - acc: 0.5000     \n",
      "Epoch 29/100\n",
      "120/120 [==============================] - 9s - loss: 1.3950 - acc: 0.5500     \n",
      "Epoch 30/100\n",
      "120/120 [==============================] - 9s - loss: 1.2612 - acc: 0.5917     \n",
      "Epoch 31/100\n",
      "120/120 [==============================] - 9s - loss: 1.1697 - acc: 0.6417     \n",
      "Epoch 32/100\n",
      "120/120 [==============================] - 9s - loss: 1.0743 - acc: 0.6833     \n",
      "Epoch 33/100\n",
      "120/120 [==============================] - 9s - loss: 1.0963 - acc: 0.6500     \n",
      "Epoch 34/100\n",
      "120/120 [==============================] - 9s - loss: 0.9886 - acc: 0.6917     \n",
      "Epoch 35/100\n",
      "120/120 [==============================] - 9s - loss: 0.9085 - acc: 0.6917     \n",
      "Epoch 36/100\n",
      "120/120 [==============================] - 9s - loss: 0.9465 - acc: 0.6917     \n",
      "Epoch 37/100\n",
      "120/120 [==============================] - 9s - loss: 0.7760 - acc: 0.7917     \n",
      "Epoch 38/100\n",
      "120/120 [==============================] - 9s - loss: 0.7133 - acc: 0.7917     \n",
      "Epoch 39/100\n",
      "120/120 [==============================] - 9s - loss: 0.6561 - acc: 0.8000     \n",
      "Epoch 40/100\n",
      "120/120 [==============================] - 9s - loss: 0.6136 - acc: 0.8167     \n",
      "Epoch 41/100\n",
      "120/120 [==============================] - 9s - loss: 0.5581 - acc: 0.8500     \n",
      "Epoch 42/100\n",
      "120/120 [==============================] - 9s - loss: 0.7516 - acc: 0.7667     \n",
      "Epoch 43/100\n",
      "120/120 [==============================] - 9s - loss: 0.5138 - acc: 0.8500     \n",
      "Epoch 44/100\n",
      "120/120 [==============================] - 9s - loss: 0.4506 - acc: 0.9000     \n",
      "Epoch 45/100\n",
      "120/120 [==============================] - 9s - loss: 0.3942 - acc: 0.8833     \n",
      "Epoch 46/100\n",
      "120/120 [==============================] - 9s - loss: 0.6240 - acc: 0.8083     \n",
      "Epoch 47/100\n",
      "120/120 [==============================] - 9s - loss: 0.3461 - acc: 0.8917     \n",
      "Epoch 48/100\n",
      "120/120 [==============================] - 9s - loss: 0.4823 - acc: 0.8500     \n",
      "Epoch 49/100\n",
      "120/120 [==============================] - 10s - loss: 0.3077 - acc: 0.9500    \n",
      "Epoch 50/100\n",
      "120/120 [==============================] - 10s - loss: 0.3378 - acc: 0.9167    \n",
      "Epoch 51/100\n",
      "120/120 [==============================] - 10s - loss: 0.3369 - acc: 0.8833    \n",
      "Epoch 52/100\n",
      "120/120 [==============================] - 9s - loss: 0.4497 - acc: 0.8583     \n",
      "Epoch 53/100\n",
      "120/120 [==============================] - 9s - loss: 0.3058 - acc: 0.9250     \n",
      "Epoch 54/100\n",
      "120/120 [==============================] - 9s - loss: 0.2792 - acc: 0.9500     \n",
      "Epoch 55/100\n",
      "120/120 [==============================] - 9s - loss: 0.2728 - acc: 0.9417     \n",
      "Epoch 56/100\n",
      "120/120 [==============================] - 9s - loss: 0.3008 - acc: 0.9250     \n",
      "Epoch 57/100\n",
      "120/120 [==============================] - 10s - loss: 0.2725 - acc: 0.9333    \n",
      "Epoch 58/100\n",
      "120/120 [==============================] - 10s - loss: 0.3019 - acc: 0.9083    \n",
      "Epoch 59/100\n",
      "120/120 [==============================] - 9s - loss: 0.2992 - acc: 0.9167     \n",
      "Epoch 60/100\n",
      "120/120 [==============================] - 9s - loss: 0.2315 - acc: 0.9333     \n",
      "Epoch 61/100\n",
      "120/120 [==============================] - 9s - loss: 0.2019 - acc: 0.9500     \n",
      "Epoch 62/100\n",
      "120/120 [==============================] - 9s - loss: 0.2232 - acc: 0.9500     \n",
      "Epoch 63/100\n",
      "120/120 [==============================] - 9s - loss: 0.2297 - acc: 0.9333     \n",
      "Epoch 64/100\n",
      "120/120 [==============================] - 9s - loss: 0.2397 - acc: 0.9167     \n",
      "Epoch 65/100\n",
      "120/120 [==============================] - 9s - loss: 0.1984 - acc: 0.9333     \n",
      "Epoch 66/100\n",
      "120/120 [==============================] - 9s - loss: 0.1865 - acc: 0.9250     \n",
      "Epoch 67/100\n",
      "120/120 [==============================] - 9s - loss: 0.2462 - acc: 0.9250     \n",
      "Epoch 68/100\n",
      "120/120 [==============================] - 9s - loss: 0.1647 - acc: 0.9583     \n",
      "Epoch 69/100\n",
      "120/120 [==============================] - 9s - loss: 0.1407 - acc: 0.9583     \n",
      "Epoch 70/100\n",
      "120/120 [==============================] - 9s - loss: 0.2484 - acc: 0.8917     \n",
      "Epoch 71/100\n",
      "120/120 [==============================] - 9s - loss: 0.1418 - acc: 0.9583     \n",
      "Epoch 72/100\n",
      "120/120 [==============================] - 9s - loss: 0.1518 - acc: 0.9500     \n",
      "Epoch 73/100\n",
      "120/120 [==============================] - 9s - loss: 0.1372 - acc: 0.9667     \n",
      "Epoch 74/100\n",
      "120/120 [==============================] - 9s - loss: 0.1458 - acc: 0.9583     \n",
      "Epoch 75/100\n",
      "120/120 [==============================] - 9s - loss: 0.1205 - acc: 0.9667     \n",
      "Epoch 76/100\n",
      "120/120 [==============================] - 9s - loss: 0.1509 - acc: 0.9417     \n",
      "Epoch 77/100\n",
      "120/120 [==============================] - 9s - loss: 0.1888 - acc: 0.9167     \n",
      "Epoch 78/100\n",
      "120/120 [==============================] - 9s - loss: 0.1468 - acc: 0.9500     \n",
      "Epoch 79/100\n",
      "120/120 [==============================] - 9s - loss: 0.2129 - acc: 0.9250     \n",
      "Epoch 80/100\n",
      "120/120 [==============================] - 9s - loss: 0.1372 - acc: 0.9583     \n",
      "Epoch 81/100\n",
      "120/120 [==============================] - 9s - loss: 0.1020 - acc: 0.9750     \n",
      "Epoch 82/100\n",
      "120/120 [==============================] - 9s - loss: 0.1189 - acc: 0.9750     \n",
      "Epoch 83/100\n",
      "120/120 [==============================] - 9s - loss: 0.1359 - acc: 0.9583     \n",
      "Epoch 84/100\n",
      "120/120 [==============================] - 9s - loss: 0.1620 - acc: 0.9250     \n",
      "Epoch 85/100\n",
      "120/120 [==============================] - 9s - loss: 0.1753 - acc: 0.9333     \n",
      "Epoch 86/100\n",
      "120/120 [==============================] - 9s - loss: 0.1077 - acc: 0.9833     \n",
      "Epoch 87/100\n",
      "120/120 [==============================] - 9s - loss: 0.0911 - acc: 0.9750     \n",
      "Epoch 88/100\n",
      "120/120 [==============================] - 9s - loss: 0.1045 - acc: 0.9583     \n",
      "Epoch 89/100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120/120 [==============================] - 9s - loss: 0.1049 - acc: 0.9667     \n",
      "Epoch 90/100\n",
      "120/120 [==============================] - 9s - loss: 0.1311 - acc: 0.9500     \n",
      "Epoch 91/100\n",
      "120/120 [==============================] - 9s - loss: 0.2061 - acc: 0.9500     \n",
      "Epoch 92/100\n",
      "120/120 [==============================] - 9s - loss: 0.1331 - acc: 0.9250     \n",
      "Epoch 93/100\n",
      "120/120 [==============================] - 9s - loss: 0.1095 - acc: 0.9667     \n",
      "Epoch 94/100\n",
      "120/120 [==============================] - 9s - loss: 0.1084 - acc: 0.9917     \n",
      "Epoch 95/100\n",
      "120/120 [==============================] - 9s - loss: 0.1551 - acc: 0.9500     \n",
      "Epoch 96/100\n",
      "120/120 [==============================] - 8s - loss: 0.1281 - acc: 0.9583     \n",
      "Epoch 97/100\n",
      "120/120 [==============================] - 9s - loss: 0.0590 - acc: 0.9917     \n",
      "Epoch 98/100\n",
      "120/120 [==============================] - 9s - loss: 0.1307 - acc: 0.9500     \n",
      "Epoch 99/100\n",
      "120/120 [==============================] - 9s - loss: 0.1090 - acc: 0.9667     \n",
      "Epoch 100/100\n",
      "120/120 [==============================] - 10s - loss: 0.1178 - acc: 0.9500    \n"
     ]
    }
   ],
   "source": [
    "model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])\n",
    "hist = model.fit(train_x, train_y, batch_size=32, epochs=100, verbose=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120/120 [==============================] - 2s     \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.033411206336071093, 0.98333333333333328]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(train_x,train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.save('faceRank.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "del model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from keras.models import load_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = load_model('faceRank.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120/120 [==============================] - 2s     \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.033411206336071093, 0.98333333333333328]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(train_x,train_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_image(img_url):\n",
    "    image = load_img(img_url,target_size=(128,128))\n",
    "    image = img_to_array(image)\n",
    "    image /= 255\n",
    "    image = np.expand_dims(image,axis=0)\n",
    "    return image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "image = load_image('data/test/9-1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([8], dtype=int64)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict_classes(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
